package com.tourting.demo.mapper;

import com.tourting.demo.model.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

@Repository
public interface UserMapper {

    @Select("SELECT * FROM users ORDER BY id DESC")
    List<User> findAll();

    @Select("SELECT * FROM users WHERE id = #{id}")
    User findById(@Param("id") long id);

    @Insert("INSERT INTO users(nickname, age, created_at) VALUES(#{nickname}, #{age}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void save(User user) throws SQLException;

    @Update("UPDATE users SET age = #{age} WHERE id = #{id}")
    int update(User user) throws SQLException;

    @Delete("DELETE FROM users WHERE id = #{id}")
    int remove(long id);

}
